import COS from 'cos-js-sdk-v5'

export default {
	/**
	 * 上传图片
	 */
	uploadImageToCos: async (token, file, path) => {
		let fileName = 'media/' + path + '/' + token.startTime + '_' + file.name;
		const cos = new COS({
			SecretId: token.credentials.tmpSecretId,
			SecretKey: token.credentials.tmpSecretKey,
			SecurityToken: token.credentials.sessionToken,
			StartTime: token.startTime,
			ExpiredTime: token.expiredTime
		});

		let response = await cos.uploadFile({
			Bucket: token.account.bucket,
			Region: token.account.region,
			Key: fileName,
			Body: file,
			SliceSize: 1024 * 1024 * 5, // 大于1mb才进行分块上传
		})
		return response;
	},
}